package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.activity.MainActivity;
import com.fanhao.dllo.soundfm.activity.PlayActivity;
import com.fanhao.dllo.soundfm.adapter.RecomViewPageAdapter;
import com.fanhao.dllo.soundfm.adapter.RecommendAdapter;
import com.fanhao.dllo.soundfm.entity.RecommendResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.tools.MyOnclickListener;
import com.fanhao.dllo.soundfm.utils.ViewPagerScrollerMaster;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

/**
 * Created by dllo on 16/1/11.
 * 推荐界面
 */
public class RecommendFragment extends BaseFragment implements MyOnclickListener {
    private ViewPager recomViewPager;
    private RecomViewPageAdapter rvpAdapter;
    private ViewPagerScrollerMaster master;
    private RecyclerView recomRv;
    private RecommendAdapter recomAdapter;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private RecyclerViewHeader rvHeader;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initView() {
        recomViewPager = findView(R.id.viewPager_recommend);
        rvpAdapter = new RecomViewPageAdapter(getActivity().getSupportFragmentManager());
        master = new ViewPagerScrollerMaster();
        master.setRunnable(recomViewPager);
        master.startTurning(5000);
        recomViewPager.setAdapter(rvpAdapter);
        recomRv = findView(R.id.rv_recommend);
        rvHeader = findView(R.id.rvHeader_recommend);

        recomRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recomAdapter = new RecommendAdapter(getActivity());
        recomRv.setAdapter(recomAdapter);
        recomAdapter.setMyOnclickListener(this);
        rvHeader.attachTo(recomRv, true);

        /**
         * 由于RecyclerView会覆盖其所在Fragment的焦点
         * 所以用RecyclerView的OnTouchListener()方法监听
         * 监听recomRv的触摸方式
         * 滑动时,隐藏播放栏
         * 停止滑动时,再次显示
         *
         */
        recomRv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    // 滑动时
                    case ACTION_MOVE:
                        MainActivity mActivity = (MainActivity) getActivity();
                        // Visibility() MainActivity 中隐藏方法
                        mActivity.Gone();
                        break;
                    // 手指移开时
                    case ACTION_UP:
                        MainActivity mainActivity = (MainActivity) getActivity();
                        // showRelative() MainActivity 中再次显示方法
                        mainActivity.Visible();
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        String url = "http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-c6&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.44.2";
        showDialog();
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        RecommendResult rr = gson.fromJson(response,
                                RecommendResult.class);
                        if (rr != null) {
                            // 获得focusList集合并通过addData方法传给rvAdapter
                            RecommendResult.FocusImagesEntity focusImagesEntity =
                                    rr.getFocusImages();
                            if (focusImagesEntity != null) {
                                List<RecommendResult.FocusImagesEntity.ListEntity> focusList =
                                        focusImagesEntity.getList();
                                if (focusList != null && focusList.size() > 0) {
                                    rvpAdapter.addData(focusList);
                                }
                            }
                            // 获得editorList集合并通过addEditorData方法传给Adapter
                            RecommendResult.EditorRecommendAlbumsEntity editor = rr.getEditorRecommendAlbums();
                            if (editor != null) {
                                List<RecommendResult.EditorRecommendAlbumsEntity.ListEntity> editorList = editor.getList();
                                if (editorList != null && editorList.size() > 0) {
                                    recomAdapter.addEditorData(editorList);
                                }
                            }
                            // 获得hot并通过addHotData方法传给Adapter
                            RecommendResult.HotRecommendsEntity hot = rr.getHotRecommends();
                            if (hot != null) {
                                recomAdapter.addHotData(hot);
                            }
                            // 获得bulletList并通过addBulletData方法传给Adapter
//                            RecommendResult.BulletAreaEntity bulletArea = rr.getBulletArea();
//                            if (bulletArea != null) {
//                                List<RecommendResult.BulletAreaEntity.ListEntity> bulletList = bulletArea.getList();
//                                if (bulletList != null && bulletList.size() > 0) {
//                                    recomAdapter.addBulletData(bulletList);
//                                }
//                            }
                            // 获得special并通过addspecialData方法传给Adapter
                            RecommendResult.SpecialColumnEntity special = rr.getSpecialColumn();
                            if (special != null) {
                                recomAdapter.addSpecialData(special);
                            }
                            // 获得discovery并通过addDiscoveryData方法传给Adapter
                            RecommendResult.DiscoveryColumnsEntity discovery = rr.getDiscoveryColumns();
                            if (discovery != null) {
                                recomAdapter.addDiscoveryData(discovery);
                            }

                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addRequest(request);
        dismissDialog();
    }

    //  // RecyclerViewAdapter 里自定义的两个参数的点击接口
    @Override
    public void myListener(long uid, int pos) {
        switch (pos) {
            // 0 跳向playList界面
            // 在跳向playList时将pos也传过去
            // 通过判断pos的值来给予不同的解析Url
            case 0:
                goToNextPlayList(new PlayListFragment(uid,pos));
                break;
            // 1 跳向playList界面
            case 1:
                goToNextPlayList(new PlayListFragment(uid,pos));
                break;
            // 2 跳向inforSubTypeTwo界面
            case 2:
                goToNextPlayList(new InforSubTypeOneFragment(uid));
                break;
        }

    }

    // RecyclerViewAdapter 里自定义的一个参数的点击接口
    @Override
    public void myNoListener(String content,int p) {
        switch (p) {
            case 0:
                goToNextPlayList(new FindNewsFragment(content));
                break;
        }
    }


}
